package com.research.project.service.impl;

import java.util.*;

import com.research.common.core.domain.entity.SysRole;
import com.research.common.core.domain.entity.SysUser;
import com.research.project.dto.ProjectDto;
import com.research.project.vo.ProjectVo;
import com.research.system.mapper.SysUserMapper;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.research.project.mapper.ProjectMapper;
import com.research.project.domain.Project;
import com.research.project.service.IProjectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目管理Service业务层处理
 * 
 * @author ghn
 * @date 2025-09-03
 */
@Service
@Slf4j
public class ProjectServiceImpl implements IProjectService 
{
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询项目管理
     *
     * @param id 项目管理主键
     * @return 项目管理
     */
    @Override
    public ProjectVo selectProjectById(Long id) {
        Project project = projectMapper.selectProjectById(id);
        return buildProjectVo(project);
    }


    /**
     * 查询项目管理列表
     *
     * @param project 项目管理
     * @return 项目管理
     */
    @Override
    public List<Project> selectProjectList(Project project) {
        // 查询项目列表
        List<Project> projects = projectMapper.selectProjectList(project);
        return projects;
    }

    /**
     * 查询项目管理Vo列表
     *
     * @param project 项目管理
     * @return 项目管理
     */
    @Override
    public List<ProjectVo> selectProjectVoList(Project project) {
        // 查询项目列表
        List<Project> projects = projectMapper.selectProjectList(project);
        List<ProjectVo> projectVoList = new ArrayList<>();

        // 遍历所有项目
        for (Project project1 : projects) {
            ProjectVo projectVo1 = buildProjectVo(project1);
            projectVoList.add(projectVo1);
        }

        return projectVoList;
    }

    /**
     * 构建 ProjectVo 对象
     *
     * @param project 项目对象
     * @return ProjectVo 对象
     */
    private ProjectVo buildProjectVo(Project project) {
        ProjectVo projectVo = new ProjectVo();
        BeanUtils.copyProperties(project, projectVo);

        // 根据项目id查询当前项目的负责人ID

        Long leader_id = projectMapper.selectUserByProjectId(project.getId());
        //根据负责人id查找其名称
        projectVo.setLeader(sysUserMapper.selectUserById(leader_id).getNickName());
        //根据项目id查询当前项目的所有成员id
        List<Long> mebmberIdList = projectMapper.selectMemberByProjectId(project.getId());
        List<String> memberList = new ArrayList<>();//用于导出姓名
        List<SysUser> memberList2 = new ArrayList<>();//用于返回vo
        for (Long member_id : mebmberIdList) {
            SysUser sysUser = sysUserMapper.selectUserById(member_id);
            memberList.add(sysUser.getNickName());
            memberList2.add(sysUser);
        }
        // 设置项目成员列表
        projectVo.setMember(memberList2);

        // 拼接字符串给 Excel 导出
        projectVo.setMemberNames(String.join(",", memberList));

        return projectVo;
    }


    /**
     * 新增项目管理
     * 
     * @param projectDto 项目管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProject(ProjectDto projectDto)
    {

        log.info("projectDto:{}",projectDto);
        Project project = new Project();
        BeanUtils.copyProperties(projectDto,project);
        int i = projectMapper.insertProject(project);//首先插入

        String leader = projectDto.getLeader();
        SysUser user_leader = sysUserMapper.selectUserByNickName(leader);//根据名称获得对象
        //log.info("!!!!!!!!!!user_leader:{}",user_leader);
        //log.info("!!!!!!!!!!user_leader.getUserId():{}",user_leader.getUserId());
        //log.info("!!!!!!!!!!project.getId():{}",project.getId());
        projectMapper.insertleader_project(user_leader.getUserId(),project.getId());//插入负责人和项目之间的关系

        List<String> members = projectDto.getMember();
        for (String member : members) {
            SysUser user_member = sysUserMapper.selectUserByNickName(member);
            projectMapper.insertmember_project(user_member.getUserId(),project.getId());//插入成员和项目之间的关系
        }
        return i;
    }

    /**
     * 修改项目管理
     * 
     * @param projectDto 项目管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateProject(ProjectDto projectDto)
    {

        Project project = new Project();
        BeanUtils.copyProperties(projectDto,project);
        String leader = projectDto.getLeader();
        //获取负责人id
        SysUser sysUser = sysUserMapper.selectUserByNickName(leader);
        //根据项目id获取负责人id
        Long leaderId = projectMapper.selectUserByProjectId(projectDto.getId());
        //负责人是否更新
        if(!sysUser.getUserId().equals(leaderId)){
            //不相等，更新负责人
            //根据项目id来更改负责人id
            projectMapper.updateLeaderProject(project.getId(),leaderId);
        }

        //获取成员的ids
        List<String> members = projectDto.getMember();
        List<Long> memberIds = new ArrayList<>();
        for (String member1 : members) {
            SysUser member = sysUserMapper.selectUserByNickName(member1);
            memberIds.add(member.getUserId());
        }
        //根据项目id获取成员的Ids
        List<Long> memberIdList = projectMapper.selectMemberByProjectId(projectDto.getId());
        //成员是否更新
        if (!new HashSet<>(memberIds).equals(new HashSet<>(memberIdList))) {
            // 成员集合不一致
            //根据项目id来修改成员
            projectMapper.deleteMember_projectByProjectId(projectDto.getId());//先删除，再增加 也是更新
            for (Long uid : memberIds) {
                projectMapper.insertmember_project( uid,projectDto.getId());
            }
        }

        return projectMapper.updateProject(project);
    }

    /**
     * 批量删除项目管理
     * 
     * @param ids 需要删除的项目管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteProjectByIds(Long[] ids)
    {
        for (Long id : ids) {
            projectMapper.deleteLeader_projectByProjectId(id);//删除项目时，同时删除项目与负责人的关系
            projectMapper.deleteMember_projectByProjectId(id);//删除项目时，同时删除项目与成员的关系
        }
        return projectMapper.deleteProjectByIds(ids);
    }

    /**
     * 删除项目管理信息
     * 
     * @param id 项目管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectById(Long id)
    {
        return projectMapper.deleteProjectById(id);
    }

    /**
     * 获取所有项目名称
     * @return
     */
    @Override
    public List<String> selectProjectName() {
        //找到项目中所有的id
        List<Long> projectIds = projectMapper.selectProjectId();
        //根据id以此查找其名称
        List<String>projectNameList = projectMapper.selectProjectNameListByIds(projectIds);
        return projectNameList;
    }
}
